from flask import Blueprint, request
from app.Service.OrderService  import OrderService
from app.Utils.response import success, error
from app.db import db

order_bp = Blueprint('order', __name__, url_prefix='/myapp/admin/order')
order_service = OrderService(db)


@order_bp.route('/list', methods=['GET'])
def list_orders():
    """分页查询订单"""
    page = request.args.get('page', type=int, default=1)
    page_size = request.args.get('pageSize', type=int, default=10)

    result = order_service.list_orders(page, page_size)
    return success(result, "订单分页查询成功")

@order_bp.route('/delete/<int:order_id>', methods=['DELETE'])
def delete_order(order_id):
    """删除订单"""
    order_service.delete_order(order_id)
    return success(None, "订单删除成功")


@order_bp.route('/delete', methods=['DELETE'])
def delete_orders():
    """批量删除订单"""
    data = request.get_json()
    ids = data.get('ids', [])

    if not ids:
        return error(400, "缺少必要参数: ids")

    order_service.delete_orders(ids)
    return success(None, "订单批量删除成功")